//
//  HJAppointmentsCell.m
//  Keping
//
//  Created by a on 2017/8/30.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "HJAppointmentsCell.h"
#import "HJAppointmentRequest.h"

@interface HJAppointmentsCell()
/** 头像 */
@property(nonatomic,weak)UIImageView*iconView;
/**
 姓名
 */
@property(nonatomic,weak)UILabel*nameLabel;
/**
 时间
 */
@property(nonatomic,weak)UILabel*dateLabel;
/**
 内容
 */
@property(nonatomic,weak)UIButton*statusBtn;
/**
 接受
 */
@property(nonatomic,weak)UIButton*acceptBtn;
/**
 拒绝
 */
@property(nonatomic,weak)UIButton*rejectBtn;

@end
@implementation HJAppointmentsCell

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString*ID=@"HJAppointmentsCell";
    HJAppointmentsCell*cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell=[[self alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        
    }
    return cell;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //添加子控件
        [self setAddSubView];
    }
    return self;
}
-(void)setAddSubView
{
    WEAKSELF;
    CGFloat margin=K_FactorW(10);
    //头像
    UIImageView*iconView=[[UIImageView alloc]init];
    [self.contentView addSubview:iconView];
    self.iconView.image = [UIImage imageNamed:@"placeholder"];
    self.iconView=iconView;
    CGFloat imgWH=K_FactorW(65);
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf).offset(margin);
        make.size.mas_equalTo(CGSizeMake(imgWH, imgWH));
        make.centerY.mas_equalTo(weakSelf);
    }];
    
    //接受按钮
    UIButton*acceptBtn=[[UIButton alloc]init];
    [acceptBtn setTitle:@"Accept" forState:UIControlStateNormal];
    [acceptBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [self.contentView addSubview:acceptBtn];
    acceptBtn.backgroundColor=[UIColor blueColor];
    [acceptBtn addTarget:self action:@selector(actionForAccept:) forControlEvents:UIControlEventTouchUpInside];
    self.acceptBtn=acceptBtn;
    acceptBtn.layer.masksToBounds = YES;
    acceptBtn.layer.cornerRadius = 4.0;
    acceptBtn.layer.borderWidth = 1.0;
    acceptBtn.layer.borderColor = kWhiteColor.CGColor;
    CGFloat btnW=K_FactorW(70);
    [acceptBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf).offset(-K_FactorW(5));
        make.top.equalTo(self.contentView.mas_top).offset(5);
        make.width.mas_equalTo(btnW);
        make.height.mas_equalTo(30);
    }];
    
    //拒绝按钮
    UIButton*rejectBtn=[[UIButton alloc]init];
    [rejectBtn setTitle:@"Reject" forState:UIControlStateNormal];
    self.rejectBtn=rejectBtn;
    [rejectBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    [rejectBtn addTarget:self action:@selector(actionForReject:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:rejectBtn];
    rejectBtn.layer.masksToBounds = YES;
    rejectBtn.layer.cornerRadius = 4.0;
    rejectBtn.layer.borderWidth = 1.0;
    rejectBtn.layer.borderColor = kGrayColor.CGColor;
    [rejectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(acceptBtn.mas_bottom).offset(5);
        make.left.right.mas_equalTo(acceptBtn);
        make.size.mas_equalTo(acceptBtn);
    }];
    
    //姓名
    UILabel*nameLabel=[[UILabel alloc]init];
    [self.contentView addSubview:nameLabel];
    nameLabel.textColor=[UIColor colorWithHexString:@"#111111"];
    nameLabel.font=[UIFont boldSystemFontOfSize:18];
    self.nameLabel=nameLabel;
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(iconView.mas_right).offset(margin);
        make.right.mas_equalTo(acceptBtn.mas_left).offset(-margin);
        make.centerY.mas_equalTo(acceptBtn).offset(-3);
    }];
    
    //时间
    UILabel*dateLabel=[[UILabel alloc]init];
    [self.contentView addSubview:dateLabel];
    dateLabel.textColor=[UIColor colorWithHexString:@"#a0a0a0"];
    dateLabel.font = [UIFont systemFontOfSize:10];
    self.dateLabel=dateLabel;
    [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(nameLabel);
        make.top.mas_equalTo(nameLabel.mas_bottom);
    }];
    
    //状态
    UIButton *statusbtn = [[UIButton alloc] init];
    [self.contentView addSubview:statusbtn];
    [statusbtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    self.statusBtn = statusbtn;
    statusbtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [statusbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dateLabel.mas_bottom).offset(5);
        make.left.equalTo(dateLabel.mas_left);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(20);
    }];
    
}

- (NSString *)getValidityDate:(NSTimeInterval )date {
    NSTimeInterval time = date / 1000.0;
    NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:time];
    NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [dateFormatter setLocale:locale]; //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"dd-MMM-yyyy hh:mm a"];
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    KLog(@"时间戳转化时间 >>> %@",currentDateStr);
    return currentDateStr;
}

- (void)setAppointment:(HJAppointmentModel *)appointment {
    _appointment = appointment;
    if (appointment.profile) {
        [self.iconView setImageWithURL:[NSURL URLWithString:[KGetImageURL stringByAppendingString:appointment.profile]] placeholder:[UIImage imageNamed:@"placeholder"]];
    } else {
        self.iconView.image = [UIImage imageNamed:@"placeholder"];
    }
    self.nameLabel.text = appointment.fullName;
    self.dateLabel.text = [self getValidityDate:[appointment.visitTime doubleValue]];
    if ([appointment.status isEqualToString:@"create"]) {//等待好友同意拜访
        self.acceptBtn.hidden = YES;
        self.rejectBtn.hidden = YES;
    } else if ([appointment.status isEqualToString:@"ing"]) {//进行中
        self.acceptBtn.hidden = YES;
        self.rejectBtn.hidden = YES;
    } else if ([appointment.status isEqualToString:@"apply"]) {//好友申请拜访
        self.acceptBtn.hidden = YES;
        self.rejectBtn.hidden = YES;
    } else if ([appointment.status isEqualToString:@"reject"]) {//拒绝
        self.acceptBtn.hidden = YES;
        self.rejectBtn.hidden = YES;
    } else {//完成
        self.acceptBtn.hidden = YES;
        self.rejectBtn.hidden = YES;
    }
    
    if ([appointment.statusStr isEqualToString:@"Pending"]) {//等待好友同意拜访
        [self.statusBtn setTitle:@"Pending" forState:UIControlStateNormal];
        [self.statusBtn setBackgroundImage:[UIImage imageNamed:@"pending"] forState:UIControlStateNormal];
    } else {
        [self.statusBtn setTitle:appointment.statusStr forState:UIControlStateNormal];
        [self.statusBtn setBackgroundImage:[UIImage imageNamed:@"upcoming"] forState:UIControlStateNormal];
    }
}

- (void)setEntity:(VisitEntityViews *)entity {
    _entity = entity;
    if (entity.profile) {
        [self.iconView setImageWithURL:[NSURL URLWithString:[KGetImageURL stringByAppendingString:entity.profile]] placeholder:[UIImage imageNamed:@"LinkTime"]];
    } else {
        self.iconView.image = [UIImage imageNamed:@"placeholder"];
    }
    
    self.nameLabel.text = entity.fullName;
    self.dateLabel.text = [self getValidityDate:entity.visitTime];
    self.acceptBtn.hidden = YES;
    self.rejectBtn.hidden = YES;
    if ([entity.statusStr isEqualToString:@"Pending"]) {//等待好友同意拜访
        [self.statusBtn setTitle:@"Pending" forState:UIControlStateNormal];
        [self.statusBtn setBackgroundImage:[UIImage imageNamed:@"pending"] forState:UIControlStateNormal];
    } else {
        [self.statusBtn setTitle:entity.statusStr forState:UIControlStateNormal];
        [self.statusBtn setBackgroundImage:[UIImage imageNamed:@"upcoming"] forState:UIControlStateNormal];
    }
}

- (IBAction)actionForAccept:(id)sender {
    if (self.hjAppointmentStatus) {
        self.hjAppointmentStatus(self.appointment, YES);
    }
}

- (IBAction)actionForReject:(id)sender {
    if (self.hjAppointmentStatus) {
        self.hjAppointmentStatus(self.appointment, NO);
    }
}

@end
